package actor.atLeastoncedelivery

/**
 * Created by DJJ on 2019/12/29.
 */

import actor.atLeastoncedelivery.CalcAggregator._
import akka.actor._
import com.typesafe.config.ConfigFactory

object AtLeastOnceDemo extends App {
  val atLeastOnceSystem = ActorSystem("atleastonceSystem", ConfigFactory.load("atLeastOnceDelivery"))

  val addActor = atLeastOnceSystem.actorOf(Calculator.props, "addActor")
  val subActor = atLeastOnceSystem.actorOf(Calculator.props, "subActor")
  val mulActor = atLeastOnceSystem.actorOf(Calculator.props, "mulActor")
  val divActor = atLeastOnceSystem.actorOf(Calculator.props, "divActor")
  var actors = Map[String, ActorRef]()
  actors += ("ADD" -> addActor)
  actors += ("SUB" -> subActor)
  actors += ("MUL" -> mulActor)
  actors += ("DIV" -> divActor)


  val aggregator = atLeastOnceSystem.actorOf(CalcAggregator.props(actors, 5), "aggregator")


  aggregator ! Sub(0, 0)
  aggregator ! Add(6, 3)
  aggregator ! Sub(8, 0)
  aggregator ! Mul(3, 2)
  /**
   * 清空日志和快照,就不会重发等待确认
   */
  //aggregator ! ClearJournal
  /**
   * 这里出现异常,之前执行的四个时间都被deliver 持久化了,因为还没触发快照,所以都是全部执行一遍
   */
  aggregator ! Boom
  aggregator ! Div(12, 3)
  Thread.sleep(10000)
  aggregator ! ShowResults


  scala.io.StdIn.readLine()
  atLeastOnceSystem.terminate()


}
